package io.itit.ecp.admin.provide.selected;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.itit.ecp.admin.provide.action.resp.CarbonSinkFactorResp;
import io.itit.ecp.admin.server.entity.CarbonSinkFactorEntity;
import io.itit.ecp.admin.server.service.ICarbonSinkFactorService;
import io.itit.grass.common.provide.action.resp.SelectedResp;
import io.itit.grass.common.server.selected.ISelected;
import io.itit.grass.common.server.utils.ConvertUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component(value = CarbonSinkFactorSelected.CODE)
public class CarbonSinkFactorSelected implements ISelected<SelectedResp> {

    public static final String CODE = "CarbonSinkFactorSelected";

    private final ICarbonSinkFactorService carbonSinkFactorService;

    @Override
    public String code() {
        return CODE;
    }

    @Override
    public List<SelectedResp> get(String param) {
        return carbonSinkFactorService.list(new LambdaQueryWrapper<CarbonSinkFactorEntity>()
                        .orderByDesc(CarbonSinkFactorEntity::getSolidifyDate))
                .stream()
                .map(item -> SelectedResp.builder()
                        .label(item.getType())
                        .value(item.getId())
                        .baseObj(ConvertUtils.convert(item, CarbonSinkFactorResp.class))
                        .build())
                .collect(Collectors.toList());
    }
}
